<template>
	<view>
		<nav-bar   fontColor="#FFFFFF" title="新标签"></nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view v-if="status">
			<view class="input_form_box">
				<view class="input_box btm_line">
					<view class="name">物品编号:</view>
					<view class="select_info1">
						<view class="input_info">
							<uni-easyinput :styles="styles" :placeholderStyle="placeholderStyle" v-model="serialNo" focus
								placeholder="请输入物品编号:"></uni-easyinput>
						</view>
					</view>
				</view>
			</view>
			<view style="margin-top: 10rpx;" v-for="(item,index) in dataList">
				<uni-list>
					<uni-list-item :index="index+1" @click="goBind(item.id)"
						:title="'物品名称:'+item.itemName + '\n物品编号:'+item.serialNo"
						:note="'案件名称:'+item.caseName + '\n'+ '案件编号:'+item.caseNo " rightText="绑卡" link="reLaunch" clickable>
					</uni-list-item>
				</uni-list>
			</view>
			<view style="height: 150rpx;">
				<view style="position: fixed;bottom: 20rpx;width: 100%;z-index: 999;">
					<button @click="search()"
						style="background: #3B5DE0;color: #FFFFFF;border-radius: 10rpx;width: 90%;height: 100rpx;margin: 0 auto;letter-spacing: 5rpx;">查询</button>
				</view>
			</view>
		</view>
		
		<view v-else>
			<view class="orderNumber">数量：{{rfidLength}}个</view>
			<view class="imgCode">
				<image class="qrImg" src="../../static/icon/scanning_ico.png"></image>
				<view class="qrTitle">请扫描RFID射频标签</view>
			</view>
			<view class="buttonBox">
				<button class="button_fix" @click="goBack" size="default">返回</button>
				<button class="button_fix" @click="submit" size="default">提交</button>
			</view>
		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="1000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :before-close="true" type="info" cancelText="取消操作" confirmText="继续绑卡" title="提示"
				content="还有其他物品需要继续绑卡吗？" @confirm="dialogConfirm()" @close="dialogClose()"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	var main = null;
	var receiver = null;
	// import uniIcons from '@/components/uni-icons/uni-icons.vue';
	import ldSelect from '@/components/ld-select/ld-select.vue'
	export default {
		components: {
			// uniIcons,
			ldSelect
		},
		data() {
			return {
				placeholderStyle: "font-size:30rpx;color:#666666;letter-spacing: 3rpx;",
				styles: {
					color: '#000000',
					borderColor: 'transparent',
					backgroundColor: '#f5f5f5',
				},
				idNo: '',
				rfid: '',
				serialNo: '',
				dataList: [],
				epc: '',
				itemId: [],
				msgType: 'success',
				messageText: '这是一条成功提示',
				rfidLength: 0,
				status: true
			};
		},
		onLoad() {
			this.get_gb();
		},
		onBackPress() {
			if (receiver != null) {
				main.unregisterReceiver(receiver)
			}
		},
		//方法
		methods: {
			dialogConfirm(){
				this.$refs.alertDialog.close()
			},
			dialogClose(){
				this.$refs.alertDialog.close()
				uni.navigateBack({
					delta: 1
				});
			},
			get_gb() {
				var that = this;
				main = plus.android.runtimeMainActivity();
				receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						var data = intent.getExtra("data");
						if (data) {
							that.rfid = data
							that.rfidLength = 1;
						}
					}
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction('android.rfid.epccode');
				var info = main.registerReceiver(receiver, filter)
				// main.unregisterReceiver(receiver)
			},
			messageToggle(type,msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open();
			},
			goBack(){
				this.rfid = '';
				this.rfidLength = 0;
				this.itemId = [];
				this.status = !this.status;
			},
			submit(){
				if (this.rfid == '') {
					this.messageToggle('error', '请扫描RFID射频标签')
					return
				} else {
					let opt = {
						url: 'oms/tbRelateItemTakeDetail/bindRfidAndPostPrint',
						method: 'POST'
					};
					let params = {
						ids: this.itemId,
						rfidCode: this.rfid
					};
					this.$http.httpTokenRequest(opt, params).then(res => {
						if (res.data.code == 200) {
							// this.$refs.popup.close()
							this.rfid = '';
							this.rfidLength = 0
							this.itemId = [];
							this.messageToggle('success', res.data.msg)
							this.$refs.alertDialog.open();
							this.status = !this.status
						} else {
							this.messageToggle('error', res.data.msg)
							this.status = !this.status
							this.rfid = '';
							this.rfidLength = 0
							this.itemId = [];
							// this.$refs.popup.close()
						}
					})
				}
			},
			search() {
				if(this.serialNo){
					let opts = {
						url: 'oms/tbRelateItemTakeDetail/search',
						method: 'POST'
					};
					let params = {
						serialNo: this.serialNo
					};
					this.$http.httpTokenRequest(opts, params).then(res => {
						console.log(res)
						if (res.data.data == '') {
							this.messageToggle('error','暂无数据')
							this.dataList = []
							return
						} else {
							this.dataList = res.data.data
							this.messageToggle('success',res.data.msg)
						}
					})
				}
			},
			goBind(e){
				this.rfid = '';
				this.epc = '';
				this.itemId = [];
				this.itemId.push(e)
				this.getRfid();
				console.log(this.itemId)
				this.status = !this.status
			},
			getRfid(){
				var that = this;
				let opt = {
					url:'api/generateRfidCode',
					method:'POST'
				};
				that.$http.httpTokenRequest(opt,{}).then(res => {
					this.epc = 'AAAA' + res.data.rfidCode
					// this.rfid = 'AAAA' + res.data.rfidCode
					// this.rfidLength = 1;
					console.log(this.epc)
					that.goPrint();
				});
			},
			goPrint(){
				let opt = {
					url:'oms/tbRelateItemTakeDetail/bindRfidAndPostPrint',
					method: 'POST'
				};
				let params = {
					ids: this.itemId,
					rfidCode: this.epc
				};
				this.$http.httpTokenRequest(opt,params).then(res => {
					
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.textArea {
		border: 1px dashed #d9d9d9;
		// color: #d9d9d9;
		border-radius: 15rpx;
		padding: 20rpx;
		margin-left: 20rpx;
		height: 100rpx;
		width: 85%;
		font-size: 30rpx;
	}

	.select {
		margin-left: 100rpx;
	}

	.scan_ico_class {
		position: absolute;
		right: 30upx;
	}

	.table_box {
		background-color: rgba($color: $themeColor, $alpha: 1);

		.table_title {
			padding: 0rpx 20rpx;
			font-size: 22rpx;
			height: 60rpx;
			line-height: 60rpx;
			color: #ffffff;
			text-align: center;
			font-weight: bold;
			background-color: rgba($color: $themeColor, $alpha: 1);
		}

		.table_head {
			display: flex;
			align-content: center;
			background-color: rgba($color: $themeColor, $alpha: 0.35);

			text {
				padding: 15rpx 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				flex-shrink: 0;
				font-size: 28rpx;
				color: #333;
				font-weight: bold;
				border-right: 2rpx solid rgba($color: $themeColor, $alpha: 0.2);

				&:last-child {
					border-right: 0;
					flex: 2;
				}
			}
		}

		.table_content {
			display: flex;
			align-content: center;
			border-top: 2rpx solid rgba($color: $themeColor, $alpha: 0.2);
			background-color: #f5ffc6;

			text {
				padding: 15rpx 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				font-size: 22rpx;
				color: #999;
				border-right: 2rpx solid rgba($color: $themeColor, $alpha: 0.3);

				&:last-child {
					border-right: 0;
					flex: 2;
				}
			}
		}
	}
	.orderNumber {
		text-align: center;
		padding: 30rpx;
		font-size: 32rpx;
		letter-spacing: 3rpx;
		font-weight: 700;
		// font-family: '宋体';
		// font-weight: ;
	}
	
	.imgCode {
		position: relative;
		background: linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) left bottom,
			linear-gradient(#000000, #000000) left bottom;
		background-repeat: no-repeat;
		background-size: 2px 20px, 20px 2px;
		height: 400rpx;
		width: 400rpx;
		margin: 0 auto;
		margin-top: 35%;
	}
	
	.txtTop {
		margin-top: 50rpx;
	}
	
	.qrImg {
		width: 250rpx;
		height: 250rpx;
		left: 20%;
		top: 15%;
	}
	
	.qrTitle {
		font-size: 28rpx;
		color: #3B5DE0;
		margin-top: 90rpx;
		text-align: center;
		letter-spacing: 3rpx;
	}
	
	.buttonBox {
		width: 70%;
		margin: 0 auto;
		display: flex;
		margin-top: 200rpx;
	}
	
	.button_fix {
		width: 40%;
		height: 70rpx;
		margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		opacity: 0.9;
		font-size: 28rpx;
		line-height: 70rpx;
	}
</style>
